//////////////////////////////////////////////////////////////////////////////////////////////
//	File			:	"PrecisionGauge.cpp"
//
//	Author			:	Mike Davis, Matt Stover, James Thornton, Keith Webster
//	
//	Project			:	TacWars
//
//	Last Modified	:	11/6/2013
//
//	Purpose			:	
//
//////////////////////////////////////////////////////////////////////////////////////////////
#include "PrecisionGauge.h"
#include "../../SGD Wrappers/CSGD_TextureManager.h"
#include "../../SGD Wrappers/CSGD_Direct3D.h"
#include "../../SGD Wrappers/CSGD_DirectInput.h"
#include "../../SGD Wrappers/SGD_String.h"
#include "../../SGD Wrappers/SGD_Math.h"
#include "../../SGD Wrappers/CSGD_EventSystem.h"
#include "../StateMachine/GameplayState.h"
#include "../Game.h"
#include "Cursor.h"
#include "Entity.h"
#include <stdlib.h>
#include <ctime>

PrecisionGauge* PrecisionGauge::GetInstance()
{
	static PrecisionGauge Instance;

	return &Instance;
}

PrecisionGauge::PrecisionGauge( void )
{
	
}

PrecisionGauge::~PrecisionGauge( void )
{
	
}

void PrecisionGauge::Enter()
{
	
	incrementor1 = 1.0f;
	incrementor2 = 1.0f;
	diff = displayTimer = 0.0f;
	atkcursorID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/sword.png"));
	sWidth = Game::GetInstance()->GetScreenWidth();
	sHeight = Game::GetInstance()->GetScreenHeight();
	target = nullptr;
	atkCursor = new Cursor;
	atkCursor->SetPosX(0);
	atkCursor->SetPosY(0);
	atkCursor->SetWidth(64);
	atkCursor->SetHeight(64);

	Hp1 = Hp2 =	Lp1 = Lp2 = hpdmg = lpdmg = cursorspot = 0;
	srand((unsigned int)time(0));

	while(incrementor1 == incrementor2)
	{
		int temp1 = rand()% 3 + 1;
		int temp2 = rand()% 3 + 1;
		incrementor1 = (float)temp1;
		incrementor2 = (float)temp2;
	}

	int tempHP = rand()% 96 + 1;
	int tempLP = rand()% 96 + 1;
	HPBar = (float)tempHP;
	LoyaltyBar = (float)tempLP;
	target = GameplayState::GetInstance()->target;
	target->AddRef();
	hpdmg = lpdmg = GameplayState::GetInstance()->selected->GetAttackDamage();
}

void PrecisionGauge::Exit()
{
	target->Release();
	atkCursor = nullptr;
}

bool PrecisionGauge::Input()
{
	switch(state)
	{
	case RUNNING:
		if(CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_RETURN) == true)
		{
			Stop();
		}
		break;
	case CRIT:
		//NO INPUT IN THIS STATE
		break;
	case CHOOSE:
		if(CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_UP) == true)
		{
			if(cursorspot - 1 <= 0)
				cursorspot = 0;
			else
				cursorspot -= 1;
		}
		if(CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_DOWN) == true)
		{
			if(cursorspot + 1 >= 2)
				cursorspot = 1;
			else
				cursorspot += 1;
		}
		if(CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_RETURN) == true)
		{
			state = CHOSEN;
		}
		break;
	case CHOSEN:
		//NO INPUT IN THIS STATE
		break;
	}

	return true;
}




void PrecisionGauge::Stop()
{
	diff = HPBar - LoyaltyBar;
	if(diff < 0)
		diff *= -1;

	if(diff <= 5)
	{
		state = CRIT;
	}
	else if(diff <= 19 && diff > 5)
	{
		hpdmg = (int)((hpdmg * (100 - diff))/100);
		lpdmg = hpdmg-10;
		if(lpdmg < 0)
			lpdmg = 0;
		state = CHOOSE;
	}
	else if(diff <= 29 && diff > 19)
	{
		hpdmg = (int)((hpdmg * (100 - diff))/100);
		lpdmg = lpdmg-hpdmg;
		if(lpdmg < 0)
			lpdmg = 0;
		state = CHOOSE;
	}
	else if(diff <= 50 && diff > 29)
	{
		hpdmg = (int)((hpdmg * (100 - diff))/100);
		lpdmg = (int)((diff/100) * (lpdmg - hpdmg));
		if(lpdmg < 0)
			lpdmg = 0;
		state = CHOOSE;
	}
	else if(diff > 50)
	{
		hpdmg = (int)((hpdmg * (100 - diff))/100);
		lpdmg = 0;
		state = CHOOSE;
	}

}



void PrecisionGauge::Update(float fElapsedTime)
{

	switch(state)
	{
	case RUNNING:
		HPBar += (int)incrementor1 * (60 * fElapsedTime);
		LoyaltyBar += (int)incrementor2 * (60 * fElapsedTime);

		if(HPBar + (incrementor1 * (60 * fElapsedTime)) > 100 || HPBar + (incrementor1 * (60 * fElapsedTime)) < 0)
			incrementor1 *= -1;
		if(LoyaltyBar + (incrementor2 * (60 * fElapsedTime)) > 100 || LoyaltyBar + (incrementor2 * (60 * fElapsedTime)) < 0)
			incrementor2 *= -1;
		break;
	case CRIT:
		displayTimer += fElapsedTime;
		if(displayTimer >= 3.0f)
		{		
			displayTimer = 0.0f;
			//Send HP LP message here
			CSGD_EventSystem::GetInstance()->SendEventNow("UNIT_DAMAGED", &hpdmg, target, nullptr);
			Game::GetInstance()->PopState();
		}
		break;
	case CHOOSE:
		//NO UPDATE IN THIS STATE
		break;
	case CHOSEN:
		displayTimer += fElapsedTime;
		if(displayTimer >= 3.0f)
		{
			displayTimer = 0.0f;			
			//Send HP LP message here
			CSGD_EventSystem::GetInstance()->SendEventNow("UNIT_DAMAGED", &hpdmg, target, nullptr);
			Game::GetInstance()->PopState();
		}

	}
}

void PrecisionGauge::Render()
{	

	switch(state)
	{
	case RUNNING:
		{
		RECT HPB = { sWidth/4, sHeight/4, sWidth/4 + 400, HPB.top + 60 };
		RECT LPB = { sWidth/4, HPB.bottom + 20, sWidth/4 + 400, LPB.top + 60 };
		RECT HPBfill = { sWidth/4, sHeight/4, sWidth/4 + (LONG)HPBar*4, HPB.top + 60 };
		RECT LPBfill = { sWidth/4, HPB.bottom + 20, sWidth/4 + (LONG)LoyaltyBar*4, LPB.top + 60 };

		CSGD_Direct3D::GetInstance()->DrawRect( HPBfill, D3DCOLOR_ARGB(255, 255, 0, 0 ));
		CSGD_Direct3D::GetInstance()->DrawRect( LPBfill, D3DCOLOR_ARGB(255, 0, 0, 255 ));
		CSGD_Direct3D::GetInstance()->DrawHollowRect( HPB, D3DCOLOR_ARGB(255, 255, 0, 0 ), 3);
		CSGD_Direct3D::GetInstance()->DrawHollowRect( LPB, D3DCOLOR_ARGB(255, 0, 0, 255 ), 3);
		break;
		}
	case CRIT:
		{
		atkCursor->SetPosX(sWidth/4);
		atkCursor->SetPosY(sHeight/4);
		ostringstream oss;
		TCHAR testwords[64];

		oss << Hp1;
		CSTR_TO_WSTR(testwords, 64, oss.str().c_str());
		RECT choicebg = { sWidth/4, sHeight/4 - 50, sWidth/4 + 400, choicebg.top + 190 };
		CSGD_Direct3D::GetInstance()->DrawRect( choicebg, D3DCOLOR_ARGB(255,0,0,0));
		CSGD_Direct3D::GetInstance()->DrawText( _T("Critical Hit!"), choicebg.left, choicebg.top, D3DCOLOR_ARGB(255,255,0,0));
		CSGD_Direct3D::GetInstance()->DrawText( _T("The enemy takes      damage"), atkCursor->GetPosX() + atkCursor->GetWidth()-20, atkCursor->GetPosY(), D3DCOLOR_ARGB(255,255,0,255));
		CSGD_Direct3D::GetInstance()->DrawText( testwords, atkCursor->GetPosX() + atkCursor->GetWidth() + 155, atkCursor->GetPosY(), D3DCOLOR_ARGB(255,255,0,255));
		CSGD_Direct3D::GetInstance()->DrawText( _T("to their HP and Loyalty!"), atkCursor->GetPosX() + atkCursor->GetWidth() + 5, atkCursor->GetPosY()+60, D3DCOLOR_ARGB(255,255,0,255));
		
		break;
		}
	case CHOOSE:
		{
		atkCursor->SetPosX(sWidth/4);
		atkCursor->SetPosY(sHeight/4);
		ostringstream oss;
		TCHAR testwords[64];

		RECT choicebg = { sWidth/4, sHeight/4 - 50, sWidth/4 + 400, choicebg.top + 190 };
		CSGD_Direct3D::GetInstance()->DrawRect( choicebg, D3DCOLOR_ARGB(255,0,0,0));
		CSGD_TextureManager::GetInstance()->Draw(atkcursorID, atkCursor->GetPosX(), atkCursor->GetPosY() + (75 * cursorspot), 0.05f, 0.05f);
		CSGD_Direct3D::GetInstance()->DrawText( _T("The enemy will take..."), choicebg.left, choicebg.top, D3DCOLOR_ARGB(255,255,0,0));

		oss << Hp1;
		CSTR_TO_WSTR(testwords, 64, oss.str().c_str());
		//first number, first row
		CSGD_Direct3D::GetInstance()->DrawText( testwords, atkCursor->GetPosX() + atkCursor->GetWidth(), atkCursor->GetPosY(), D3DCOLOR_ARGB(255,255,0,255));
		//second number, second row
		CSGD_Direct3D::GetInstance()->DrawText( testwords, atkCursor->GetPosX() + (atkCursor->GetWidth() * 4), atkCursor->GetPosY() + 75, D3DCOLOR_ARGB(255,255,0,255));

		CSGD_Direct3D::GetInstance()->DrawText( _T(" HP damage and "), atkCursor->GetPosX() + atkCursor->GetWidth() + 20, atkCursor->GetPosY(), D3DCOLOR_ARGB(255,255,0,255));
		CSGD_Direct3D::GetInstance()->DrawText( _T(" LP damage and "), atkCursor->GetPosX() + atkCursor->GetWidth() + 20, atkCursor->GetPosY() + 75, D3DCOLOR_ARGB(255,255,0,255));
		oss.clear();
		oss.str("");
		oss << Lp1;
		CSTR_TO_WSTR(testwords, 64, oss.str().c_str());

		//first number, second row
		CSGD_Direct3D::GetInstance()->DrawText( testwords, atkCursor->GetPosX() + atkCursor->GetWidth(), atkCursor->GetPosY() + 75, D3DCOLOR_ARGB(255,255,0,255));
		//second number, first row
		CSGD_Direct3D::GetInstance()->DrawText( testwords, atkCursor->GetPosX() + (atkCursor->GetWidth()*4), atkCursor->GetPosY(), D3DCOLOR_ARGB(255,255,0,255));

		CSGD_Direct3D::GetInstance()->DrawText( _T(" LP damage!"), atkCursor->GetPosX() + atkCursor->GetWidth()*4 + 20, atkCursor->GetPosY(), D3DCOLOR_ARGB(255,255,0,255));
		CSGD_Direct3D::GetInstance()->DrawText( _T(" HP damage!"), atkCursor->GetPosX() + atkCursor->GetWidth()*4 + 20, atkCursor->GetPosY() + 75, D3DCOLOR_ARGB(255,255,0,255));
		
		break;
		}
	case CHOSEN:
		{
		atkCursor->SetPosX(sWidth/4);
		atkCursor->SetPosY(sHeight/4);
		ostringstream oss;
		TCHAR testwords[64];

		oss << hpdmg;
		CSTR_TO_WSTR(testwords, 64, oss.str().c_str());
		RECT choicebg = { sWidth/4, sHeight/4 - 50, sWidth/4 + 400, choicebg.top + 190 };
		CSGD_Direct3D::GetInstance()->DrawRect( choicebg, D3DCOLOR_ARGB(255,0,0,0));
		CSGD_Direct3D::GetInstance()->DrawText( _T("The enemy takes      HP damage"), atkCursor->GetPosX() + atkCursor->GetWidth()-20, atkCursor->GetPosY(), D3DCOLOR_ARGB(255,255,0,255));
		CSGD_Direct3D::GetInstance()->DrawText( testwords, atkCursor->GetPosX() + atkCursor->GetWidth() + 155, atkCursor->GetPosY(), D3DCOLOR_ARGB(255,255,0,255));
		oss.clear();
		oss.str("");
		oss << lpdmg;
		CSTR_TO_WSTR(testwords, 64, oss.str().c_str());
		CSGD_Direct3D::GetInstance()->DrawText( _T("and      Loyalty damage!"), atkCursor->GetPosX() + atkCursor->GetWidth() + 5, atkCursor->GetPosY()+60, D3DCOLOR_ARGB(255,255,0,255));
		CSGD_Direct3D::GetInstance()->DrawText( testwords, atkCursor->GetPosX() + atkCursor->GetWidth() + 55, atkCursor->GetPosY()+60, D3DCOLOR_ARGB(255,255,0,255));
		}
	}
}